<template>
  <div>
    <div class="row">
      <div v-for="column in columnList" :key="column._id" class="col-4 mb-4">
        <div class="card h-100 shadow-sm" style="width: 18rem">
          <div class="card-body text-center">
            <img
              :src="column.avatar && column.avatar.url"
              alt="..."
              class="rounded-circle border border-light w-25 my-3"
            />
            <h5 class="card-title">{{ column.title }}</h5>
            <p class="card-text">{{ column.description }}</p>
            <!--            <a href="#" class="btn btn-primary">进入专栏</a>-->
            <router-link
              :to="`/column/${column._id}`"
              class="btn btn-outline-primary"
              >进入专栏</router-link
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, PropType } from "vue";
import { ColumnProps } from "@/store";

export default defineComponent({
  name: "ColmunList",
  props: {
    list: {
      // 此处的 Array 并不是变量类型，而是构造函数
      // 通过 PropType 指定构造函数的类型
      type: Array as PropType<ColumnProps[]>,
      required: true, //必填项
    },
  },
  setup(props) {
    // console.log(props)
    // console.log(props.list)
    //通过computed处理没有数据时的默认图片
    const columnList = computed(() => {
      return props.list.map((column) => {
        if (!column.avatar) {
          column.avatar = {
            url: require("@/assets/logo.png"),
          };
        }
        return column;
      });
    });
    return {
      columnList,
    };
  },
});
</script>
